package euler.p051_100;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import euler.MainEuler;
import euler.helper.Utils;

public class Euler082 extends MainEuler {

    /*
        The minimal path sum in the 5 by 5 matrix below,
        by starting in any cell in the left column and finishing
        in any cell in the right column, and only moving up, down,
        and right, is indicated in red and bold; the sum is equal to 994.


        131 673 234 103 18
        201 96  342 965 150
        630 803 746 422 111
        537 699 497 121 956
        805 732 524 37  331


        Find the minimal path sum, in http://projecteuler.net/project/matrix.txt,
        a 31K text file containing a 80 by 80 matrix,
        from the left column to the right column.
     */
    public String resolve() throws IOException {

        int length = 80;

        int[][] matrix = new int[length][];

        String filePath = Utils.pathToFiles + "matrix.txt";

        BufferedReader br = null;

        try {
            br = new BufferedReader(new FileReader(new File(filePath)));
            String strLine;
            int i = 0;
            while ((strLine = br.readLine()) != null)   {

                String[] split = strLine.split(",");

                int[] row = new int[split.length];

                for (int j = 0; j < row.length; j++) {
                    row[j] = Integer.valueOf(split[j]);
                }

                matrix[i++]=row;
            }
        } finally {
            if (br != null) {
                br.close();
            }
        }

        int[][] minimalMatrix = new int[length][length];

        for (int k = 0; k < length; k++) {
            minimalMatrix[k][0] = matrix[k][0];
        }

        for (int k = 1; k < length; k++) {
            for (int i = 0; i < length; i++) {
                minimalMatrix[i][k] = matrix[i][k] + minimalMatrix[i][k-1];
            }

            boolean cambio = true;
            while (cambio) {
                cambio = false;

                for (int i = 0; i < length; i++) {
                    int min;
                    if (i == 0) {
                        min = matrix[i][k] + minimalMatrix[i+1][k];
                    } else if (i == length - 1) {
                        min = matrix[i][k] + minimalMatrix[i-1][k];
                    } else {
                        min = Math.min(minimalMatrix[i+1][k], minimalMatrix[i-1][k]) + matrix[i][k];
                    }

                    if (min < minimalMatrix[i][k]) {
                        cambio = true;
                        minimalMatrix[i][k] = min;
                    }
                }
            }
        }

        int min = minimalMatrix[0][length-1];
        for (int k = 1; k < length; k++) {
            min = Math.min(min, minimalMatrix[k][length-1]);
        }

        return String.valueOf(min);
    }
}
